package org.softmed.neodatis.util.id;

import java.util.HashMap;
import java.util.Map;
import org.softmed.neodatis.util.id.generator.IDGenerator;
import org.softmed.neodatis.util.reflection.ClassIDFieldRegistration;

public class ClassFieldIDGenerators {
	Map<String, Map<String, IDGenerator>> generators = new HashMap<String, Map<String, IDGenerator>>();

	public synchronized Object getNextID(Class type, String field, Object object) {
		Map<String, IDGenerator> fieldGens = generators.get(type
				.getCanonicalName());
		IDGenerator generator = fieldGens.get(field);
		Object id = generator.generate(object);
		return id;
	}

	public synchronized void registerGenerator(Class type, String fieldName,
			IDGenerator generator, boolean replace) throws Throwable {

		Map<String, IDGenerator> fieldGens = generators.get(type
				.getCanonicalName());
		if (fieldGens == null) {
			fieldGens = new HashMap<String, IDGenerator>();
			generators.put(type.getCanonicalName(), fieldGens);
		}

		if (!replace && fieldGens.get(fieldName) != null)
			return;

		fieldGens.put(fieldName, generator);
	}

}
